package practise.alogrithm.hundredInterviewQuestions;

import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * get the largest K numbers in a array
 */
public class GetLargestNumber {

    public List<Integer> getLargestNumbers(int[] values, int k){
        if(values.length<k){
            throw new IllegalArgumentException("number amount must larger than value length!");
        }
        PriorityQueue<Integer> maxTop = new PriorityQueue<Integer>(k);
        for(int i : values){
            addInMaxQueue(i, maxTop, k);
        }
        return new ArrayList(maxTop);
    }

    private void addInMaxQueue(Integer i,PriorityQueue<Integer> maxTop, int size){
        if(maxTop.size()<size){
            maxTop.offer(i);
        }
        else{
            maxTop.offer(Math.max(i,maxTop.poll()));
        }
    }

    public static void main(String[] args){
        GetLargestNumber getLargestNumber = new GetLargestNumber();
        System.out.print("result:" +
                getLargestNumber.getLargestNumbers(new int[]{1, 3, 4, 6, 8,  9, 4, 5, 2}, 3));
    }

}
